import java.util.Arrays;

import org.junit.Test;

public class MyTest {
    static final int MOD = 1000000007;
    @Test
    public void test(){
        int[] nums1={2,4,6,8,10};
        int[] nums2={2,4,6,8,10};
        System.out.println(minAbsoluteSumDiff(nums1, nums2));
    }
    public int minAbsoluteSumDiff(int[] nums1, int[] nums2) {
        int sum=0;
        int[] result=new int[nums1.length];
        for(int i=0;i<nums1.length;i++){
            result[i]=Math.abs(nums1[i]-nums2[i]);
            sum+=result[i];
        }
        Arrays.sort(nums1);
        int max=0;
        for(int i=0;i<result.length;i++){
            if(result[i]-find(nums1, nums2[i])>max){
                max=result[i]-find(nums1, nums2[i]);
            }
        }
        return sum-max;
    }
    int find(int[] nums1,int value){
        int left=0;
        int right=nums1.length;
        int mid=0;
        while(left<right){
            mid=(left+right)/2;
            if(value<nums1[mid]){
                right=mid;
            }else if(value>nums1[mid]){
                left=mid+1;
            }else{
                return 0;
            }
        }
        return Math.min(Math.abs(value-nums1[right]), Math.abs(value-nums1[left]));
    }
}
